package com.bwda.dsrs.filecenter.service.impl;

import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.dbinfo.DbInfoCondition;
import com.bwda.dsrs.filecenter.domain.po.dbinfo.DbInfoPo;
import com.bwda.dsrs.filecenter.mapper.dsrs.DbInfoMapper;
import com.bwda.dsrs.filecenter.service.DbInfoService;
import com.bwda.dsrs.filecenter.service.IRemoteService;
import com.bwda.dsrs.filecenter.util.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class DbInfoServiceImpl extends BaseServiceImpl<DbInfoPo, DbInfoCondition, DbInfoMapper> implements DbInfoService {

    @Autowired
    private DbInfoMapper dbInfoMapper;
    @Autowired
    private OracleRemoteServiceImpl oracleRemoteService;

    private final Logger logger = LoggerFactory.getLogger(DbInfoServiceImpl.class);

    @Override
    public void batchDelete(String ids) {
        String[] dbid = ids.split(",");
        for (String str : dbid) {
            this.getMapper().delete(Integer.parseInt(str));
        }
    }

    @Override
    public Boolean testConnection(Integer dbId) {
        Boolean connectFlag = false;
        DbInfoCondition dbInfoCondition = new DbInfoCondition();
        dbInfoCondition.setDbId(dbId);
        dbInfoCondition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
        List<DbInfoPo> dbInfoPoList = dbInfoMapper.selectList(dbInfoCondition);
        if (!CollectionUtils.isEmpty(dbInfoPoList) && dbInfoPoList.get(0) != null) {
            Connection connection;
            IRemoteService iRemoteService = SpringUtils.getBean(dbInfoPoList.get(0).getDbType().concat("RemoteServiceImpl"));
            try {
                connection = iRemoteService.login(dbInfoPoList.get(0));
                int timeoutSeconds = 6;
                if (connection.isValid(timeoutSeconds)) {
                    connectFlag = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return connectFlag;
    }

    @Override
    public Map<Boolean, String> testConnections(Integer dbId) {
        Map<Boolean, String> map = new HashMap<>(16);
        DbInfoCondition dbInfoCondition = new DbInfoCondition();
        dbInfoCondition.setDbId(dbId);
        dbInfoCondition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
        List<DbInfoPo> dbInfoPoList = dbInfoMapper.selectList(dbInfoCondition);
        if (!CollectionUtils.isEmpty(dbInfoPoList) && dbInfoPoList.get(0) != null) {
            Connection connection;
            IRemoteService iRemoteService = SpringUtils.getBean(dbInfoPoList.get(0).getDbType().concat("RemoteServiceImpl"));
            try {
                connection = iRemoteService.login(dbInfoPoList.get(0));
                int timeoutSeconds = 10;
                if (connection.isValid(timeoutSeconds)) {
                    map.put(true, "连接成功");
                }
//				List<String> dbList = oracleRemoteService.getAllDBList(connection,dbInfoPoList.get(0));
//				logger.error("ip{}的{}数据库版本为{}", dbInfoPoList.get(0).getDbIp(), dbInfoPoList.get(0).getServiceName(),connection.getMetaData().getDatabaseProductVersion());
//				for (String dbName:dbList
//					 ) {
//					logger.error("ip{}的{}实例名下的库{}表数量为{}", dbInfoPoList.get(0).getDbIp(), dbInfoPoList.get(0).getServiceName(),dbName,oracleRemoteService.countTable(connection,dbName));
//				}
            } catch (SQLException sqlException) {
                map.put(false, sqlException.toString());
                logger.error("e:{}", sqlException.getLocalizedMessage());
            } catch (Exception e) {
                map.put(false, "未知异常，请和管理员联系！" + e.getLocalizedMessage());
                logger.error("{}", e);
            }
        }
        return map;
    }

    @Override
    public List<DbInfoPo> queryLists(DbInfoCondition condition) {
        List<DbInfoPo> list = dbInfoMapper.selectList(condition);
        return list;
    }
}